package com.fireline.beauty.seting;

import java.math.BigDecimal;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Spinner;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;

import com.fireline.beauty.bean.FwxmVo;
import com.fireline.beauty.image.ImageUtil;
import com.fireline.beauty.jdbc.SqliteJdbcImpl;
import com.zgw.fireline.base.dataset.Dataset;
import com.zgw.fireline.base.widgets.ControlUtil;

/**
 * 套餐包含服务项目设置
 * */
public class TcfwxmDlg extends Dialog {

	protected int result = SWT.CANCEL;
	protected Shell shell;
	private Text textXmbh;
	private Spinner textFwcs;
	private Spinner textHlzq;
	private Text textYhdj;
	private Text textSsje;
	private Text textBz;
	private CLabel labDj;
	private CLabel labLb;
	private CLabel labXmmc;
	// ========================
	// 业务数据
	// ===========================
	public FwxmVo fwxm;
	private String xmbh;

	/**
	 * Create the dialog.
	 * 
	 * @param parent
	 * @param style
	 */
	public TcfwxmDlg(Shell parent, int style) {
		super(parent, style);
		setText("SWT Dialog");
	}

	public TcfwxmDlg(Shell parent, int style, String xmbh) {
		super(parent, style);
		this.xmbh = xmbh;

	}

	/**
	 * Open the dialog.
	 * 
	 * @return the result
	 */
	public int open() {
		createContents();
		Display display = getParent().getDisplay();
		ControlUtil.centerShell(shell);
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.PRIMARY_MODAL);
		shell.setSize(457, 284);
		shell.setText("增加服务项目");
		shell.setLayout(new GridLayout(1, false));

		Group group = new Group(shell, SWT.NONE);
		group.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1,
				1));
		group.setText("服务项目信息");
		GridLayout gl_group = new GridLayout(5, false);
		gl_group.marginWidth = 25;
		gl_group.horizontalSpacing = 0;
		gl_group.verticalSpacing = 15;
		group.setLayout(gl_group);

		Label lblNewLabel = new Label(group, SWT.NONE);
		lblNewLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		lblNewLabel.setText("项目编号：");

		Composite composite_1 = new Composite(group, SWT.NONE);
		composite_1.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false,
				false, 1, 1));
		GridLayout gl_composite_1 = new GridLayout(1, false);
		gl_composite_1.verticalSpacing = 0;
		gl_composite_1.horizontalSpacing = 0;
		gl_composite_1.marginHeight = 0;
		gl_composite_1.marginWidth = 0;
		composite_1.setLayout(gl_composite_1);

		textXmbh = new Text(composite_1, SWT.BORDER);
		textXmbh.setToolTipText("请输入编号、名称、助记词进行查找\r\n按↓键直接显示所有行");

		// Listener 确认选中记录后触发该事件
		final FwxmTextPopup textPopup = new FwxmTextPopup(textXmbh,
				new Listener() {
					@Override
					public void handleEvent(Event event) {
						Dataset set = (Dataset) event.data;
						doSelectedXm(set);

					}
				});
		GridData gd_text = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		gd_text.widthHint = 110;
		textXmbh.setLayoutData(gd_text);

		Label lblNewLabel_4 = new Label(group, SWT.NONE);
		lblNewLabel_4.setToolTipText("点击查找");
		lblNewLabel_4.setText("查找");
		lblNewLabel_4.setImage(ImageUtil.getImage("query.png", 22, 22));
		lblNewLabel_4.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				if (!textPopup.isShow()) {
					// textPopup.doFilter(textXmbh.getText().trim());
					textPopup.show("");
				}
			}
		});
		Label lblNewLabel_1 = new Label(group, SWT.NONE);
		GridData gd_lblNewLabel_1 = new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1);
		gd_lblNewLabel_1.horizontalIndent = 30;
		lblNewLabel_1.setLayoutData(gd_lblNewLabel_1);
		lblNewLabel_1.setText("项目名称：");

		labXmmc = new CLabel(group, SWT.NONE);
		labXmmc.setFont(SWTResourceManager.getFont("宋体", 9, SWT.BOLD));
		labXmmc.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLUE));
		labXmmc.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));
		labXmmc.setText("");

		Label label = new Label(group, SWT.NONE);
		label.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false,
				1, 1));
		label.setText("所属类别：");

		labLb = new CLabel(group, SWT.NONE);
		labLb.setFont(SWTResourceManager.getFont("宋体", 9, SWT.BOLD));
		labLb.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLUE));
		labLb.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1,
				1));
		labLb.setText("");
		new Label(group, SWT.NONE);

		Label label_1 = new Label(group, SWT.NONE);
		label_1.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false,
				1, 1));
		label_1.setText("单价：");

		labDj = new CLabel(group, SWT.NONE);
		labDj.setFont(SWTResourceManager.getFont("宋体", 9, SWT.BOLD));
		labDj.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1,
				1));
		labDj.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLUE));
		labDj.setText("");

		Group group_1 = new Group(shell, SWT.NONE);
		GridLayout gl_group_1 = new GridLayout(4, false);
		gl_group_1.marginWidth = 25;
		gl_group_1.marginHeight = 10;
		gl_group_1.horizontalSpacing = 0;
		gl_group_1.verticalSpacing = 15;
		group_1.setLayout(gl_group_1);
		group_1.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));
		group_1.setText("服务次数设置");

		Label lblNewLabel_3 = new Label(group_1, SWT.NONE);
		lblNewLabel_3.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		lblNewLabel_3.setText("服务次数：");

		// 根据服务次数 和 优惠单价次数 计算
		final Listener calcSsje = new Listener() {
			@Override
			public void handleEvent(Event event) {
				if (textYhdj.getText().trim().equals(""))
					return;
				Float dj;
				try {
					dj = Float.parseFloat(textYhdj.getText());
					textSsje.setText("" + dj * textFwcs.getSelection());
				} catch (NumberFormatException e1) {
					textSsje.setText("");
				}
			}
		};
		textFwcs = new Spinner(group_1, SWT.BORDER);
		textFwcs.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false,
				1, 1));
		textFwcs.addListener(SWT.Modify, calcSsje);
		Label label_4 = new Label(group_1, SWT.NONE);
		GridData gd_label_4 = new GridData(SWT.RIGHT, SWT.CENTER, false, false,
				1, 1);
		gd_label_4.horizontalIndent = 40;
		label_4.setLayoutData(gd_label_4);
		label_4.setText("护理周期：");

		textHlzq = new Spinner(group_1, SWT.BORDER);
		textHlzq.setToolTipText("单位：天");
		textHlzq.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false,
				1, 1));

		Label label_5 = new Label(group_1, SWT.NONE);
		label_5.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false,
				1, 1));
		label_5.setText("优惠单价：");

		textYhdj = new Text(group_1, SWT.BORDER);
		textYhdj.setFont(SWTResourceManager.getFont("宋体", 10, SWT.BOLD));
		textYhdj.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
		textYhdj.addListener(SWT.Modify, calcSsje);
		GridData gd_textYhdj = new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1);
		gd_textYhdj.widthHint = 125;
		textYhdj.setLayoutData(gd_textYhdj);
		ControlUtil.setCurrencyRule(textYhdj, false, false);

		Label label_6 = new Label(group_1, SWT.NONE);
		label_6.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false,
				1, 1));
		label_6.setText("实收金额：");

		textSsje = new Text(group_1, SWT.BORDER);
		textSsje.setFont(SWTResourceManager.getFont("宋体", 10, SWT.BOLD));
		textSsje.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
		GridData gd_textSsje = new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1);
		gd_textSsje.widthHint = 125;
		textSsje.setLayoutData(gd_textSsje);
		ControlUtil.setCurrencyRule(textSsje, false, false);

		Label label_7 = new Label(group_1, SWT.NONE);
		label_7.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false,
				1, 1));
		label_7.setText("备注信息：");

		textBz = new Text(group_1, SWT.BORDER);
		textBz.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 3,
				1));

		Composite composite = new Composite(shell, SWT.NONE);
		RowLayout rl_composite = new RowLayout(SWT.HORIZONTAL);
		rl_composite.spacing = 20;
		composite.setLayout(rl_composite);
		composite.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));

		Button btnNewButton_1 = new Button(composite, SWT.NONE);
		btnNewButton_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				okPressed();
			}
		});
		btnNewButton_1.setLayoutData(new RowData(70, SWT.DEFAULT));
		btnNewButton_1.setText("确定");

		Button btnNewButton = new Button(composite, SWT.NONE);
		btnNewButton.setLayoutData(new RowData(70, SWT.DEFAULT));
		btnNewButton.setText("退出");
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				result = SWT.CANCEL;
				fwxm = null;
				shell.close();
			}
		});
		if (xmbh != null) {
			Dataset dataset = new Dataset("select * from fwxm where xmbh=?",
					SqliteJdbcImpl.INSTANCE);
			dataset.setParam(0, xmbh);
			dataset.update();
			if (dataset.getCount() <= 0) {
				MessageDialog.openWarning(shell, "警告", "找不到编号为:" + xmbh + ""
						+ "的服务项目");
			} else {
				dataset.setSelected(0);
				doSelectedXm(dataset);
			}
		} else if (fwxm != null) {
			initializeData(fwxm);
		}
	}

	/*
	 * 选定一个项目
	 */
	protected void doSelectedXm(Dataset set) {
		String xmbh = set.getString("xmbh");
		String xmmc = set.getString("xmmc");
		String dj = set.getString("dj");
		String lb = set.getString("lb");
		Integer zqsj = set.getValue("zqsj", Integer.class);
		textXmbh.setText(xmbh == null ? "" : xmbh);
		labDj.setText(dj == null ? "" : dj);
		textYhdj.setText(dj == null ? "" : dj);
		labXmmc.setText(xmmc == null ? "" : xmmc);
		labLb.setText(lb == null ? "" : lb);
		textXmbh.setSelection(textXmbh.getCharCount(), textXmbh.getCharCount());
		textFwcs.setSelection(1);
		textHlzq.setSelection(zqsj == null ? 0 : zqsj);
		textXmbh.setData(xmbh);
	}

	private void initializeData(FwxmVo fwxmvo) {

		textXmbh.setText(fwxm.xmbh == null ? "" : fwxm.xmbh);
		labDj.setText(fwxm.dj == null ? "" : fwxm.dj);
		textYhdj.setText(fwxm.yhdj == null ? "" : fwxm.yhdj);
		labXmmc.setText(fwxm.xmmc == null ? "" : fwxm.xmmc);
		labLb.setText(fwxm.lb == null ? "" : fwxm.lb);
		textXmbh.selectAll();
		textSsje.setText(fwxm.ssje == null ? "" : fwxm.ssje);
		textFwcs.setSelection(fwxm.fwcs);
		textHlzq.setSelection(fwxm.hlzq);
		textBz.setText(fwxm.bz == null ? "" : fwxm.bz);
		textXmbh.setData(fwxm.xmbh);
	}

	public boolean isNull(String str) {
		return str == null || str.trim().equals("");
	}

	protected void okPressed() {
		if (textXmbh.getData() == null) {
			MessageDialog.openQuestion(shell, "提示", "请选择一项服务项目");
			textXmbh.setFocus();
			return;
		}
		if (fwxm == null) {
			fwxm = new FwxmVo();
		}
		fwxm.xmbh = (String) textXmbh.getData();
		fwxm.xmmc = labXmmc.getText();
		fwxm.lb = labLb.getText();
		fwxm.dj = labDj.getText();
		fwxm.fwcs = Integer.parseInt(textFwcs.getText());
		fwxm.hlzq = Integer.parseInt(textHlzq.getText());
		fwxm.yhdj = textYhdj.getText();
		fwxm.hj = new BigDecimal(fwxm.yhdj).multiply(new BigDecimal(fwxm.fwcs))
				.toString();
		fwxm.ssje = textSsje.getText();
		fwxm.bz = textBz.getText();
		result = SWT.OK;
		shell.close();
	}

	public static void main(String[] args) {
		TcfwxmDlg dlg = new TcfwxmDlg(new Shell(), SWT.NONE);
		dlg.open();
	}
}
